#include "Menu.h"
namespace SLG
{
	Menu::Menu(void)
		:Manager("Menu")
	{
		m_cursor=-1;
		m_bradius=0;
		m_battlePos=D3DXVECTOR3(-1000.0f,-1000.0f,-1000.0f);
	}

	Menu::~Menu(void)
	{
	}

	bool Menu::HasObj(int32 id)
	{
		for(vector<PICTURE>::iterator iter=m_data.begin();iter<m_data.end();iter++)
		{
			if(iter->tid==id)
				return true;
		}
		return false;
	}

	bool Menu::AddBattle(D3DXVECTOR3 pos)
	{
		m_battlePos=pos;
		D3DXVECTOR2 spos=m_coord->WorldToScreenCoord(&m_battlePos);
		UpdateBattle(spos);
		return true;
	}

	bool Menu::UpdateBattle()
	{
		if(m_battlePos==D3DXVECTOR3(-1000.0f,-1000.0f,-1000.0f))
			return false;
		D3DXVECTOR2 spos=m_coord->WorldToScreenCoord(&m_battlePos);
		UpdateBattle(spos);
		return true;
	}

	bool Menu::UpdateBattle(D3DXVECTOR2 pos)
	{

		for(vector<PICTURE>::iterator iter=m_data.begin();iter<m_data.end();iter++)
		{
			if(iter->tid>=1000&&iter->tid<2000)
			{
				switch(iter->tid)
				{
				case BATTLE_DISK:
					iter->pos=D3DXVECTOR2(pos.x-iter->info.Width/2,pos.y-iter->info.Height/2);
					iter->draw=true;
					break;
				case BATTLE_ATTACK:
					iter->pos=D3DXVECTOR2(pos.x+(m_bradius-iter->info.Width/2),pos.y-iter->info.Height/2);
					iter->draw=true;
					break;
				case BATTLE_COMSKILL:
					iter->pos=D3DXVECTOR2(pos.x+(m_bradius/2-iter->info.Width/2),pos.y-m_bradius*1.73/2-iter->info.Height/2);
					iter->draw=true;
					break;
				case BATTLE_GUARK:
					iter->pos=D3DXVECTOR2(pos.x-(m_bradius/2+iter->info.Width/2),pos.y-m_bradius*1.73/2-iter->info.Height/2);
					iter->draw=true;
					break;
				case BATTLE_MAGIC:
					iter->pos=D3DXVECTOR2(pos.x+(m_bradius/2-iter->info.Width/2),pos.y+m_bradius*1.73/2-iter->info.Height/2);
					iter->draw=true;
					break;
				case BATTLE_MOVE:
					iter->pos=D3DXVECTOR2(pos.x-(m_bradius/2+iter->info.Width/2),pos.y+m_bradius*1.73/2-iter->info.Height/2);
					iter->draw=true;
					break;
				case BATTLE_PROPS:
					iter->pos=D3DXVECTOR2(pos.x-(m_bradius+iter->info.Width/2),pos.y-iter->info.Height/2);
					iter->draw=true;
					break;
				default:
					break;
				}
			}
		}
		return true;
	}

	bool Menu::UpdatePos(int32 id,D3DXVECTOR2 pos)
	{
		if(m_data.size()<=id)
			return false;
		m_data[id].pos=pos;
		return true;
	}

	bool Menu::DrawPic(int32 id)
	{
		if(m_data.size()<=id)
			return false;
		m_data[id].draw=true;
		return true;
	}

	bool Menu::UnDrawPic(int32 id)
	{
		if(m_data.size()<=id)
			return false;
		m_data[id].draw=false;;
		return true;
	}

	bool Menu::UpdateMousePos(D3DXVECTOR2 pos)
	{
		if(m_cursor==-1)
		{
			return false;
		}
		if(UpdatePos(m_cursor,pos)&&DrawPic(m_cursor))
			return true;
		else
			return false;
	}

	bool Menu::AddMenu(int32 id,char* fname)
	{
		if(HasObj(id))
			return false;
		PICTURE tmp;
		tmp.id=m_cur++;
		tmp.tid=id;
		tmp.draw=false;
		tmp.pos=D3DXVECTOR2(0,0);
		if(FAILED(D3DXCreateTextureFromFileEx(g_pd3dDevice,fname, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,0,D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,0xFF000000,&tmp.info,NULL,&tmp.tex)))
		{
			return false;
		}
		m_data.push_back(tmp);
		if(id==CURSOR)
			m_cursor=tmp.id;
		if(id==BATTLE_DISK)
			m_bradius=tmp.info.Width/2-7.0f;
		return true;
	}

	void Menu::Log()
	{
	}
}